package script.mainpage
{
	import eventUtil.EventCenter;
	
	import laya.events.Event;
	import laya.ui.Button;
	import laya.ui.View;
	
	import model.ConstantModel;
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.Userdata;
	
	import script.ViewManager;
	import script.baseView.BaseViewController;
	
	import ui.MainPagePanelUI;
	import ui.tabview.CategoryPanelUI;
	import ui.tabview.ChooseSubjectPanelUI;
	import ui.tabview.HomePanelUI;
	import ui.tabview.UserCartPanelUI;
	import ui.tabview.UserCenterPanelUI;
	
	public class MainPageController extends BaseViewController
	{
		private var uiSkin:MainPagePanelUI;
		
		private var tabbtns:Array = [];
		
		private var viewArr:Array;
		private var curView:View;

		//private var hasOpenView:Array;
		private var curViewIndex:int = -1;

		public function MainPageController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as MainPagePanelUI;
			
			viewArr = [HomePanelUI,CategoryPanelUI,UserCartPanelUI,UserCenterPanelUI];

			tabbtns.push(uiSkin.home);
			tabbtns.push(uiSkin.category);
			tabbtns.push(uiSkin.cart);
			tabbtns.push(uiSkin.user);

			//hasOpenView = new Array(tabbtns.length);
			onClickTabView(0,null);
			for(var i:int=0;i < tabbtns.length;i++)
			{
				tabbtns[i].on(Event.CLICK,this,onClickTabView,[i]);
				
			}
			refreshGoodsCount();
			EventCenter.instance.on(EventCenter.SHOW_CONTENT_PANEL,this,changeContentPanel);
			EventCenter.instance.on(EventCenter.UPDATE_USER_CART_NUM,this,refreshGoodsCount);

		}
		
		private function changeContentPanel(viewIndex:int,paramdata:Object):void
		{
			onClickTabView(viewIndex,paramdata);
		}
		private function onClickTabView(index:int,data:Object=null):void
		{
			
			
			if(curViewIndex == index)
				return;
			curViewIndex = index;
			
			if(index == 2 || index ==3)
			{
				if(!Userdata.instance.isLogin)
				{
					ViewManager.showAlert("请先登录");
					ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);

					return;
				}
			}
				
			while(uiSkin.tancontainer.numChildren > 0)
			{
				uiSkin.tancontainer.removeChildAt(0);
				
			}
			for(var i:int=0;i < tabbtns.length;i++)
			{
				(tabbtns[i] as Button).selected = i==index;
			}
//			if(hasOpenView[index] != null)
//			{
//				uiSkin.tancontainer.addChild(hasOpenView[index]);
//
//			}
			 if(viewArr[index])
			{
				 if(curView)
				 curView.destroy(true);
				 
				curView = new viewArr[index]();
				
				if(data)
					curView.param = data;
				
				uiSkin.tancontainer.addChild(curView);
				
				//hasOpenView[index] = curView;
			}
			
		}
		
		private function refreshGoodsCount():void
		{
			if(Userdata.instance.isLogin)
				HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getCartGoodsCount,this,onGetGoodsCountBack,null,"post");
			else
			{
				uiSkin.goodsCount.visible = false;
				uiSkin.goodsCountlbl.text = "0";
			}
			
		}
		private function onGetGoodsCountBack(data:Object):void
		{
			if(uiSkin.destroyed)
				return;
			var result:Object = JSON.parse(data as String);
			
			if(result.code == HttpErrorCode.OK)
			{
				if(result.data.num > 0)
				{
					uiSkin.goodsCount.visible = true;
					uiSkin.goodsCountlbl.text = result.data.num + "";
				}
				else
				{
					uiSkin.goodsCount.visible = false;
					uiSkin.goodsCountlbl.text = "0";

				}
			}
		}
		
		public override function onDestroy():void
		{
			EventCenter.instance.off(EventCenter.SHOW_CONTENT_PANEL,this,changeContentPanel);

		}
		
		
	}
}